﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;
using AppCommon;
using Controller;
using Model;

public partial class ACPanel_HomeAdmin : System.Web.UI.Page
{
    public static SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid("NgayCapNhat", false);
            if (Request.Cookies["VYCTravel"] != null)
            {
                // Ok
                // Response.Redirect("~/ACPanel/HomeAdmin.aspx");                
            }
            else
            {
                // Nhan parameter tu form Login
                var userName = Request.Params["name"];
                var passWord = Request.Params["password"];

                if (userName != null && passWord != null)
                {
                    CheckLogin(userName.Replace("'", "''"), passWord.Replace("'", "''"));
                }
                else
                {
                    Response.Redirect("~/ACPanel/");
                }
            }        
        }
    }

    /// <summary>
    /// Method : FillGrid 
    /// Use : Bind data to gridview
    /// </summary>
    /// <param name="SortExpression"></param>
    /// <param name="IsSorting"></param>
    public void FillGrid(string SortExpression, bool IsSorting)
    {
        var dt = new DataTable();

        // Lay chuoi ket noi CSDL
        Connection.Open();
        const string query = "SELECT * FROM [VYC-20] ORDER BY NgayCapNhat DESC";
        var dataAdapter = new SqlDataAdapter(query, Connection);
        dataAdapter.Fill(dt);
        Connection.Close(); // dong ket noi       

        var dv = dt.DefaultView;
        if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
        {
            if (IsSorting)
            {
                if (ViewState["SortExpression"].ToString() == SortExpression)
                {
                    if (ViewState["SortDirection"].ToString() == "DESC")
                        ViewState["SortDirection"] = "ASC";
                    else
                        ViewState["SortDirection"] = "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = SortExpression;
                    ViewState["SortDirection"] = "DESC";
                }
            }
        }
        else
        {
            ViewState["SortExpression"] = SortExpression;
            ViewState["SortDirection"] = "DESC";
        }

        dv.Sort = ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        gv.DataSource = dv.ToTable();
        gv.DataBind();
    }

    /// <summary>
    /// Event OnPageIndexChanging="gv_PageIndexChanging" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        FillGrid(ViewState["SortExpression"].ToString(), false);
    }


    public void CheckLogin(string userName, string passWord)
    {
        var user = new ModelVYC17();
        var passHash = Common.Encrypt(passWord + userName);
        user.UserName = userName;
        user.PasswordHash = passHash;

        var controller = new VYC17Controller();
        var listUser = controller.CheckLogin(user);

        if (listUser.Count <= 0)
        {
            Response.Redirect("~/ACPanel/");
        }
        else
        {
            // Them vao Session
            Session.Add(Constants.SESSION_ADMIN, listUser[0]);
            Session.Timeout = 90;
            // Thiet lap cookie
            var userCookie = new HttpCookie("VYCTravel");
            userCookie["Title"] = Common.Encrypt(listUser[0].UserName);
            userCookie["Desc"] = Common.Encrypt(listUser[0].PasswordHash);
            userCookie["Content"] = Common.Encrypt(listUser[0].HoTen);
            userCookie.Expires = DateTime.Today.AddDays(1);
            Response.Cookies.Add(userCookie);
            // Luu log
            var logInfo = new ModelVYC20();
            var logConroller = new VYC20Controller();

            logInfo.Ten = "Đăng nhập";
            logInfo.ThongTin = "Đăng nhập trang quản trị thành công vào lúc " + DateTime.Now.ToLongDateString();
            logInfo.TrangThai = 1;
            logInfo.UserName = userName;

            logConroller.AddData(logInfo);
        }
    }


    public void BindDataDetail()
    {
        // Get luot xem, luot truy cap

        // 
    }
}